package leetcode.offer_two_special.backtrack;

import java.util.LinkedList;
import java.util.List;

/**
 * @author mazouri
 * @create 2021-09-20 12:14
 */
public class _110_allPathsSourceTarget {
    List<List<Integer>> res = new LinkedList<>();
    LinkedList<Integer> track = new LinkedList<>();
    int[][] graph;
    int target;

    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        this.target = graph.length-1;
        this.graph = graph;
        dfs(0);
        return res;
    }

    private void dfs(int x) {
        if (x == this.target){
            res.add(new LinkedList<>(track));
            return;
        }

        for (int y : graph[x]) {
            track.add(y);
            dfs(y);
            track.removeLast();
        }
    }
}
